package my.assistant.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
import java.util.Set;

public class ResUtils
{
	private final static String CONF_DIR = "/sdcard/.myAssistant/profile/";
	private final static String CONF_FILE = "/sdcard/.myAssistant/profile/conf";
	private final static Properties prop = new Properties();
	static
	{
		InputStream in = null;
		try
		{
			File dir = new File(CONF_DIR);
			if (dir.exists() == false)
				dir.mkdirs();
			File conf = new File(CONF_FILE);
			if (conf.exists() == false)
				conf.createNewFile();
			in = new FileInputStream(CONF_FILE);
			prop.load(in);
		} catch (Exception e)
		{
			throw new RuntimeException(e.getMessage());
		} finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				} catch (IOException e)
				{
					throw new RuntimeException(e.getMessage());
				}
			}
		}
	}

	public final static boolean getBooleanRes(String key)
	{
		String v = prop.getProperty(key);
		if (v != null)
		{
			return Boolean.valueOf(v);
		}
		return false;
	}

	public final static int getIntRes(String key)
	{
		return Integer.valueOf(getStringRes(key));
	}

	public final static String getStringRes(String key)
	{
		String v = prop.getProperty(key);
		if (v != null)
		{
			return v;
		}
		return "";
	}

	public final static boolean hasKeyPrefix(String keyPrefix)
	{
		Set<Object> keySet = prop.keySet();
		for (Object object : keySet)
		{
			if (String.valueOf(object).startsWith(keyPrefix))
				return true;
		}
		return false;
	}

	public final static void setRes(String key, Object obj, boolean hibernate)
	{
		if (obj instanceof String)
		{
			prop.setProperty(key, (String) obj);
		} else if (obj instanceof boolean[])
		{
			prop.setProperty(key, ArrayUtils.toString((boolean[])obj));
		} else
		{
			prop.setProperty(key, String.valueOf(obj));
		}
		if (hibernate)
		{
			saveConf();
		}
	}

	public final static void setRes(String key, Object obj)
	{
		setRes(key, obj, true);
	}

	public final static void saveConf()
	{
		PrintStream out = null;
		try
		{
			out = new PrintStream(CONF_FILE);
			prop.list(out);
			out.flush();
		} catch (FileNotFoundException e)
		{
			throw new RuntimeException(e.getMessage());
		} finally
		{
			if (out != null)
			{
				out.close();
			}
		}
	}

	public static void delRes(String s)
    {
		Set<Object> keySet;
		boolean has = false;
		while (true)
        {
			has = false;
	        keySet = prop.keySet();
	        for (Object o : keySet)
	        {
		        if (o.toString().startsWith(s))
		        {
			        prop.remove(o);
			        has = true;
			        break;
		        }
	        }
	        if (has == false)
	        {
	        	break;
	        }
        }
		saveConf();
    }
}
